<?php
namespace app\admin\controller;

use app\admin\service\Novel as NovelService;
use think\facade\Request;
use think\facade\View;

class Novel extends Controller
{
    public function index()
    {
        if (Request::isAjax()) {
            $condition = [];
            $sex =  input("sex");
            if(!empty($sex)){
                $condition[] = ["sex", "=", $sex];
            }
            $keyword =  input("keyword");
            if(!empty($keyword)){
                $condition[] = ['title','like',"%".$keyword."%"];
            }
            $state =  input("state", "");
            if($state != ""){
                $condition[] = ['state','=',$state];
            }
            $status =  input("status", "");
            if($status != ""){
                $condition[] = ['status','=',$status];
            }
            $sensitive =  input("is_sensitive", "");
            if($sensitive != ""){
                $condition[] = ['is_sensitive','=',$sensitive];
            }
            $attr =  input("attr", "");
            if($attr != ""){
                $condition[] = ['attr','=',$attr];
            }
            $vip =  input("is_vip", "");
            if($vip != ""){
                $condition[] = ['is_vip','=',$vip];
            }
            $chargeType =  input("charge_type", "");
            if($chargeType != ""){
                $condition[] = ['charge_type','=',$chargeType];
            }
            $word =  input("word", "");
            if($word != ""){
                $condition[] = ['word','=',$word];
            }
            $switch =  input("push_switch", "");
            if($switch != ""){
                $condition[] = ['push_switch','=',$switch];
            }
            $orderBy =  input("order_by", "");
            if($orderBy != ""){
                $condition[] = ['order_by','=',$orderBy];
            }
            View::assign('search', input());
            $category = new NovelService();
            $cateList = $category->getList($condition);
            return $this->success("获取成功", $cateList);
        }else {
            return View::fetch();
        }
    }

    public function add()
    {
        $category = new NovelService();
        if (Request::isAjax()) {
            $sex = input("sex", 1);//性别： 1：女 2：男
            $pid = input("pid", 0);//上级分类id
            $name = input("name");
            $sort = input("sort", 99);
            $status = input("status", 1);
            $data = [
                "sex" => $sex,
                "pid" => $pid,
                "name" => $name,
                "sort" => $sort,
                "status" => $status,
            ];
            $res = $category->add($data);
            if (!$res) $this->error($category->getError());
            return $this->success('添加成功');
        }else {
            $cateList = $category->getList(["pid" => 0]);
            View::assign('cate_list', $cateList['list']);
            return View::fetch();
        }
    }

    public function edit()
    {
        $category = new NovelService();
        if (Request::isAjax()) {
            $post = input();
            $res = $category->edit($post, ["id" => $post['id']]);
            if (!$res) $this->error($category->getError());
            return $this->success('修改成功');
        } else {

            $id = input('id');
            if (empty($id)) return $this->error('非法操作');
            $info = $category->detail($id);

            if (empty($info)) return $this->error('菜单不存在');
            View::assign('info', $info);

            $cateList = $category->getList(["pid" => 0]);
            View::assign('cate_list', $cateList['list']);
            return View::fetch('add');
        }
    }

    public function del()
    {
        if (Request::isAjax()) {
            $ids = input('id');
            if (empty($ids)) return $this->error('请选择删除项');
            $category = new NovelService();
            $res = $category->del($ids);
            if (!$res) $this->error($category->getError());
            return $this->success('删除成功');
        }
    }

}
